package com.msb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.msb.constant.TokenConstant;
import com.msb.dto.LoginTicketDTO;
import com.msb.dto.UserDTO;
import com.msb.enums.LoginTicketStatusEnum;
import com.msb.mapper.TitcketMapper;
import com.msb.mapper.UserMapper;
import com.msb.params.LoginParams;
import com.msb.params.RegisterParams;
import com.msb.result.Result;
import com.msb.service.TokenService;
import com.msb.utils.Md5Util;
import com.msb.utils.UUid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.logging.Logger;

/**
 * @ClassName: TokenServiceImpl
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2024/8/4-13:23
 * @Version 1.0
 */
@Service
public class TokenServiceImpl implements TokenService {
    private Logger logger = Logger.getLogger("TokenServiceImpl");
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TitcketMapper titcketMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result register(RegisterParams params) {
        logger.info("开始注册:{}"+ JSONObject.toJSONString(params));
        String username = params.getUsername();
        if(username == null || username.isEmpty()) {
            return Result.error("用户名不能为空");
        }
        String password = params.getPassword();
        if(password == null || password.isEmpty()) {
            return Result.error("密码不能为空");
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setId(UUid.createUUid());
        userDTO.setUsername(username);
        String salt = UUid.createUUid().substring(0,6);
        userDTO.setSalt(salt);
        // 设置password = password + salt 然后进行md5加密
        userDTO.setPassword(Md5Util.getMD5String( password+ salt));
        try {
            // 添加用户
            userMapper.addUserDTO(userDTO);
            logger.info("用户注册成功");
            // 添加ticket
            LoginTicketDTO loginTicketDTO = new LoginTicketDTO();
            loginTicketDTO.setId(UUid.createUUid());
            loginTicketDTO.setUserId(userDTO.getId());
            loginTicketDTO.setTicket(UUid.createUUid());
            // 设置 ticket 过期时间时当前时间+3天
            Long time = new Date().getTime() + TokenConstant.TICKET_EXPIRED_TIME;
            loginTicketDTO.setExpiredDate(new Date(time));
            loginTicketDTO.setStatus(LoginTicketStatusEnum.NORMAL.getStatus());
            titcketMapper.addTicket(loginTicketDTO);
            logger.info("用户添加ticket成功");

            return Result.success(loginTicketDTO.getTicket());
        }catch (Exception e){
            logger.info("发生异常");
            e.printStackTrace();
            Result.error("发生异常");
            throw e;
        }

    }

    @Override

    public Result login(LoginParams params) {
        logger.info("开始注册:{}"+ JSONObject.toJSONString(params));
        String username = params.getUsername();
        if(username == null || username.isEmpty()) {
            return Result.error("用户名不能为空");
        }

        UserDTO userDTO = userMapper.queryUserDTO(username);
        if (userDTO == null) {
            logger.info("用户不存在");
            return Result.error("用户不存在");
        }
        String password = params.getPassword();
        // 首先通过用户名去查密码
        // 然后用密码进行校验是否正确
        if(!userDTO.getPassword().equals(Md5Util.getMD5String(password + userDTO.getSalt()))) {
            // 密码不相等
            logger.info("密码不正确");
            return Result.error("密码不正确");
        }
        // 能走到这里说明用户名 密码都有并且密码校验通过
        // 开始更新令牌
        // 1.  通过userid 拿到存储的ticket
        LoginTicketDTO loginTicketDTO = titcketMapper.queryTicketByUserId(userDTO.getId());
//        String password = params.getPassword();
//        if(password == null || password.isEmpty()) {
//            return Result.error("密码不能为空");
//        }
        //
        return  null;
    }

}
